package com.ocom.wxapi.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.request.wx.MachInfoPageOnShowRequest;
import com.ocom.common.request.wx.MachInfoPageRemoveMachRequest;
import com.ocom.common.request.wx.MachInfoPageUpdateNameRequest;
import com.ocom.common.request.wx.MachInfoPageUpdatePlaceRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.wx.MachInfoPageOnShowResponse;
import com.ocom.common.utils.ResultUtil;
import com.ocom.wxapi.client.AccessClientService;
import com.ocom.wxapi.client.AiotClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@PreAuthorize("hasRole('ROLE_WX')")
@RequestMapping("/MachInfoPage")
@RestController
public class MachInfoPageController {

    @Autowired
    AccessClientService accessClientService;

    @Autowired
    AiotClientService aiotClientService;


    Logger logger= LoggerFactory.getLogger(MachInfoPageController.class);



    @PostMapping("/onShow")
    public Result onShow(@Valid MachInfoPageOnShowRequest request) {

        Result result=accessClientService.getMachineInfoById(request.getCompanyId(),request.getMachNo());


        MachInfoPageOnShowResponse machInfoPageOnShowResponse;

        if(result.getCode()!=200){
            return ResultUtil.error(result.getCode());
        }

        machInfoPageOnShowResponse = JSON.parseObject(JSON.toJSONString(result.getData()),MachInfoPageOnShowResponse.class);

        Map<String, String> pageFormat=new HashMap<>();
        pageFormat.put("pageTitle","设备信息+");
        pageFormat.put("lblExNoCaption","出厂号：");
        pageFormat.put("lblNameCaption","设备名称：");
        pageFormat.put("cmdNameCaption","修改名称");
        pageFormat.put("lblPlaceCaption","安装位置：");
        pageFormat.put("cmdPlaceCaption","修改安装位置");
        pageFormat.put("cmdWIFICaption","设置WIFI参数");
        pageFormat.put("cmdVerifyCaption","验证设备");
        machInfoPageOnShowResponse.setPageFormat(pageFormat);

        return ResultUtil.success(machInfoPageOnShowResponse);
    }


    @PostMapping("/updateName")
    public Result updateName(@Valid MachInfoPageUpdateNameRequest request) {
        Result result=accessClientService.updateMachNameByMachNo(request.getCompanyId(),request.getMachNo(),request.getMachName());
        if(result.getCode()!=200){
            return ResultUtil.error(result.getCode());
        }
        EntityMjDeviceInfo entityMjDeviceInfo= JSON.parseObject(JSON.toJSONString(result.getData()), EntityMjDeviceInfo.class);
        Map<String,String> map=new HashMap<>();
        map.put("machName",entityMjDeviceInfo.getDeviceName());
        return ResultUtil.info(200,"出厂号为"+entityMjDeviceInfo.getDeviceNo()+"的设备已更名为："+entityMjDeviceInfo.getDeviceName()+"。",map);

    }


    @PostMapping("/updatePlace")
    public Result updatePlace(@Valid MachInfoPageUpdatePlaceRequest request) {
        Result result=accessClientService.changeMachPlace(request.getCompanyId(),request.getMachNo(),request.getAreaNo());
        return result;
    }



    @PostMapping("/removeMach")
    public Result removeMach(@Valid MachInfoPageRemoveMachRequest request) {
        Result result=accessClientService.removeMach(request.getCompanyId(),request.getMachNo());

        //aiot 修改
        try {
            Map<String,Object> map =new HashMap<>();
            map.put("saasNo","2");
            map.put("comId",request.getCompanyId());
            map.put("agentId",0);
            map.put("status",0);
            map.put("deviceNo",request.getMachNo());
            aiotClientService.SetMjDeviceStatusBySaas(map);
        }catch (Exception e){
            logger.error("aiot删除机器异常 【{}】",e);
        }

        if(result.getCode()!=200){
            return ResultUtil.error(result.getCode());
        }
        return result;
    }




}
